/*  Copyright 2010 Daniel Brasil Magnani
 *  Created on: Dec 20, 2010
 *  Author: daniel */

#include "instance.h"

int Instance::Init(char *instance_name) {
  strcpy(instance_name_, instance_name);
  ReadPaintInput();
  ReadConstraints();
  ReadVehicles();
  return true;
}

int Instance::ReadPaintInput() {
  char temp_string[256];
  FILE *paint_limit_input;
  sprintf(temp_string, "instances/%s/paint_batch_limit.txt\0", instance_name_);

  paint_limit_input = fopen(temp_string, "r");
  if ( paint_limit_input == NULL )
    return false;

  fscanf(paint_limit_input, "%*s\n%d;\n", &paint_batch_limit_);
  fclose(paint_limit_input);
  return true;
}

int Instance::ReadConstraints() {
  int n, p;
  int priority;
  char temp_string[256];
  FILE *ratio_input;
  sprintf(temp_string, "instances/%s/ratios.txt\0", instance_name_);
  ratio_input = fopen(temp_string, "r");
  if ( ratio_input == NULL )
    return false;
  Constraint temp_constraint;
  fscanf(ratio_input, "%*s\n");
  while ( fscanf(ratio_input, "%d/%d;%d;%*s\n", &n, &p, &priority) > 0 ) {
    temp_constraint.n = n;
    temp_constraint.p = p;
    temp_constraint.priority = priority;
    constraint_list_.push_back(temp_constraint);
  }
  fclose(ratio_input);
  return true;
}

int Instance::ReadVehicles() {
  int date;
  int date_before = 0;
  int date_change = 0;
  int64_t identifier;
  int color;
  Car temp_car;
  int temp_int;

  FILE *vehicles_input;
  char temp_string[256];
  sprintf(temp_string, "instances/%s/vehicles.txt\0", instance_name_);
  vehicles_input = fopen(temp_string, "r");
  if ( vehicles_input == NULL )
    return false;
  fscanf(vehicles_input, "%*s%*s\n");
  while ( fscanf(vehicles_input, "%*s%*s%d;%*d;%lld;%d;",
                 &date, &identifier, &color) > 0 ) {
    temp_car.identifier = identifier;
    temp_car.color = color;
    temp_car.constraints = new int[constraint_list_.size()];

    if ( (date != date_before && date_before != 0) )
      date_change = 1;

    for ( int i = 0; i < constraint_list_.size() ; i++ ) {
      fscanf(vehicles_input, "%d;", &temp_int);
      if ( temp_int ) {
        temp_car.constraints[i] = 1;
      } else {
        temp_car.constraints[i] = 0;
      }
    }

    if ( date_change == 0 )
      yesterday_cars_.push_back(temp_car);
    else
      today_cars_.push_back(temp_car);

    date_before = date;
  }
  fclose(vehicles_input);
  return true;
}

int Instance::WriteOutput(const std::vector<Car>& solution) {
  FILE *output;
  char temp_string[256];
  sprintf(temp_string, "outputs/%s\0", instance_name_);
  output = fopen(temp_string, "w");
  if ( output == NULL )
    return false;

  for ( int i = yesterday_cars_.size(); i < solution.size() ; i++ ) {
    fprintf(output, "%d;%lld\n",
            i-yesterday_cars_.size()+1,
            solution[i].identifier);
  }


  fclose(output);
  return true;
}

char* Instance::get_instance_name() {
  return instance_name_;
}

int Instance::get_paint_batch_limit() {
  return paint_batch_limit_;
}

const std::vector<Car>& Instance::get_yesterday_cars() {
  return yesterday_cars_;
}

const std::vector<Car>& Instance::get_today_cars() {
  return today_cars_;
}

const std::vector<Constraint>& Instance::get_constraint_list() {
  return constraint_list_;
}
